package com.hope.hotel.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hope.hotel.entity.DAO.LoginDAO;
import com.hope.hotel.entity.DAO.TravelerDAO;
import com.hope.hotel.entity.dto.UserInfoDTO;
import com.hope.hotel.entity.po.Traveler;
import com.hope.hotel.entity.po.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;
import java.util.Map;

/**
 * @作者 TD
 * @日期 2025/5/18
 */

@Mapper
public interface UserMapper extends BaseMapper<User> {


    @Insert("insert into traveler (user_id, name, phone, id_card) values (#{userId}, #{traveler.name}, #{traveler.phone}, #{traveler.idCard})")
    void insertTraveler(@Param("traveler")Traveler traveler, @Param("userId") String userId);

    @Select("SELECT exists(SELECT 1 FROM traveler WHERE user_id = #{userId} AND name = #{traveler.name} AND phone = #{traveler.phone} AND id_card = #{traveler.idCard} LIMIT 1)")
    boolean checkTravelerIsExist(
            @Param("traveler") TravelerDAO travelerDAO,
            @Param("userId") String userId
    );

    @Select("SELECT * FROM traveler WHERE user_id = #{userId}")
    List<Traveler> selectTravers(String userId);

    @Select("SELECT * FROM users WHERE username = #{loginDAO.username} AND password = #{loginDAO.password} AND role = 'admin' LIMIT 1")
    User checkAdminLogin(@Param("loginDAO") LoginDAO loginDAO);


    @Select("SELECT username,avatar from users where user_id = ${userId}")
    UserInfoDTO selectUserInfo(Long userId);
    
    /**
     * 更新旅客信息
     * @param traveler 旅客信息
     * @return 影响的行数
     */
    @Update("UPDATE traveler SET name = #{name}, id_card = #{idCard}, phone = #{phone} WHERE id = #{id}")
    int updateTraveler(Traveler traveler);
    
    /**
     * 删除旅客信息
     * @param id 旅客ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM traveler WHERE id = #{id}")
    int deleteTraveler(String id);
    
    /**
     * 检查旅客是否属于当前用户
     * @param id 旅客ID
     * @param userId 用户ID
     * @return 是否属于
     */
    @Select("SELECT EXISTS(SELECT 1 FROM traveler WHERE id = #{id} AND user_id = #{userId} LIMIT 1)")
    boolean checkTravelerBelongsToUser(@Param("id") String id, @Param("userId") String userId);
}
